package com.education.online.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.config.BusinessException;
import com.education.common.config.CommonErrorCode;
import com.education.common.pojo.LCatalog;
import com.education.common.pojo.LCatalogMenu;
import com.education.common.utils.ApiReturnObject;
import com.education.online.feign.CatalogFeign;
import com.education.online.pojo.OCatalogLesson;
import com.education.online.pojo.OCatalogMenu;
import com.education.online.service.OCatalogLessonService;
import com.education.online.service.OCatalogMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.Id;
import java.util.ArrayList;
import java.util.List;

/**
 * 专业课程体系表
 *
 * @date 2020-06-04 15:17:49
 */
@Api(tags = {"PC--专业课程体系表"})
@RestController
@RequestMapping("/online/ocataloglesson")
public class OCatalogLessonController {

    @Autowired
    private OCatalogLessonService oCatalogLessonService;

    @Autowired
    private OCatalogMenuService oCatalogMenuService;


    @ApiOperation(value = "查询全部课程", httpMethod = "GET")
    @GetMapping("/selectList")
    public ApiReturnObject selectList(Integer type, Integer userId, Integer catalogId, Integer pageNum, Integer pageSize) throws BusinessException {
        if (pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        if (userId == null || type == null) {
//            return new ApiReturnObject("201", "failure", "参数缺失");
            throw new BusinessException(CommonErrorCode.E_200101);

        }
        //tyep : 1 查询已购买        0 : 查询未购买
        if (type == 1) {
            List<OCatalogLesson> oCatalogLessonList = oCatalogLessonService.selectOCatalogLessonByUserId(userId, catalogId);
            PageInfo<OCatalogLesson> pageInfo = new PageInfo<>(oCatalogLessonList);
            return new ApiReturnObject("200", "success", pageInfo);
        } else if (type == 0) {
            List<OCatalogLesson> oCatalogLessonList = oCatalogLessonService.selectNotOCatalogLessonByUserId(userId, catalogId);
            PageInfo<OCatalogLesson> pageInfo = new PageInfo<>(oCatalogLessonList);
            return new ApiReturnObject("200", "success", pageInfo);
        }
//        return new ApiReturnObject("202", "failure", "type传参不正确");
        throw new BusinessException(CommonErrorCode.E_200102);
    }



    @ApiOperation(value = "目录筛选", httpMethod = "GET")
    @GetMapping("/CatalogFilter")
    public ApiReturnObject catalogFilter(Integer menuId,Integer userId,Integer type){
        if (userId == null || type == null || menuId == null) {
//            return new ApiReturnObject("201", "failure", "参数缺失");
            throw new BusinessException(CommonErrorCode.E_200101);
        }
        List<OCatalogMenu> oCatalogMenuList = oCatalogMenuService.selectOCatalogMenuByParentId(menuId);
        //判断该目录是否是一级目录 未查询到  说明是二级目录  否则是一级
        if (oCatalogMenuList.size() == 0){
            //查询二级目录下的所有课程
            List<Integer> arrayList = new ArrayList<>();
            arrayList.add(menuId);
            //tyep : 1 查询已购买        0 : 查询未购买
            return getIsBuy(userId, type, arrayList);
        }else {
            //查询一级目录下的所有课程
            List<Integer> arrayList = new ArrayList<>();
            for (OCatalogMenu oCatalogMenu : oCatalogMenuList) {
                arrayList.add(oCatalogMenu.getId());
            }
            return getIsBuy(userId, type, arrayList);
        }
    }

    /**
     * 目录筛选使用 :  查询是否购买
     * @param userId
     * @param type
     * @param arrayList
     * @return
     */
    private ApiReturnObject getIsBuy(Integer userId, Integer type, List<Integer> arrayList) {
        if (type == 1) {
            List<OCatalogLesson> oCatalogLessonList = oCatalogLessonService.selectBuyOCatalogLessonByTwoMenuId(arrayList, userId);
            return new ApiReturnObject("200", "success", oCatalogLessonList.size(), oCatalogLessonList);
        } else if (type == 0){
            List<OCatalogLesson> oCatalogLessonList = oCatalogLessonService.selectNotBuyOCatalogLessonByTwoMenuId(arrayList, userId);
            return new ApiReturnObject("200", "success", oCatalogLessonList.size(), oCatalogLessonList);
        }
//        return new ApiReturnObject("202", "failure", "type传参不正确");
        throw new BusinessException(CommonErrorCode.E_200102);
    }

    @ApiOperation(value = "课程搜索", httpMethod = "GET")
    @GetMapping("/CatalogSearch")
    public ApiReturnObject catalogSearch(String catalogName){
        List<OCatalogLesson> oCatalogLessonList = oCatalogLessonService.selectOCatalogLessonByCatalogName(catalogName);
        return new ApiReturnObject("200","success",oCatalogLessonList.size(),oCatalogLessonList);
    }

}
